﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web;
using Googler.BLL;
using Googler.DAL;

namespace Googler.BLL
{
    public class TimeLogger : IDisposable
    {
        Stopwatch s;
        TimeSpan span;
        string codeBlockTitle;
        int ident;

        /// <summary>
        /// Initialize new instance of time logger
        /// </summary>
        /// <param name="codeBlockTitle">Title of the code block being measured</param>
        public TimeLogger(string codeBlockTitle)
        {
            this.codeBlockTitle = codeBlockTitle;
            s = new Stopwatch();
            s.Start();
        }

        /// <summary>
        /// Initialize new instance of time logger
        /// </summary>
        /// <param name="codeBlockTitle">Title of the code block being measured</param>
        /// <param name="ident">How many tabs to add before the notification message</param>
        public TimeLogger(string codeBlockTitle,int ident):this(codeBlockTitle)
        {
            this.ident = ident;
        }
        public void Dispose()
        {
            s.Stop();
            span = new TimeSpan(s.ElapsedTicks);
            Sessions.NotificationMessages.Add(new NotificationMessage()
            {
                 Text = this.ToString(),
                 Sender = this
            });
        }
        public override string ToString()
        {
            string text = "";
            for (int i = 0; i < ident; i++)
            {
                text += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            text += codeBlockTitle;
            return string.Format("{0} takes {1}", text, span.ToHumman());
        }
    }
}
